﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Net;
using HRM.Common;

    public class DataWCFHelper : IDataRemoteHelper
    {
        // Fields
        //private int ꮽ� 韸䈘㳂䔶;
        private string _connectionString;
        //private int ካꤣ׫ᖨ﫜싛䱝;
        //private const string ᱩ꣥砫ၨ㭋�쌻 = "Remote server is not available or can't connect to remote server";
        //private int 怊펎꛹䇼踡冭ṣ;
        private Exception exception;
        //private IDataWCF iDataWCF;
        //private ManualResetEvent manualResetEvent;
        //
        wcf_rest_sqlcommand sqlcmd;

        public int Begin_Trans()
        {
            return 0;
        }

        public bool Commit_Trans(int transID)
        {
            return true;
        }

        public string Config_Read(string sectionKey, bool sectionEncode)
        {
            return "";
        }

        public bool Config_Write(string sectionKey, string sectionValue, bool sectionEncode)
        {
            return true;
        }

        public bool Connect(IPEndPoint remoteEndPoint, int timeoutMSec)
        {
            return true;
        }

        public bool Connection_Valid(string connectString)
        {
            return true;
        }

        //--Execute Dataset
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------

        public DataSet ExecuteDataset(int iTransID, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataSet ExecuteDataset(int iTransID, string spName, params object[] parameterValues)
        {
            return null;
        }

        public DataSet ExecuteDataset(string connectString, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataSet ExecuteDataset(string connectString, string spName, params object[] parameterValues)
        {
            InitCommand(connectString, spName, parameterValues);
            return (DataSet)sqlcmd.ExecuteDataSet();
        }

        private void InitCommand(string connectString, string sQuery)
        {
            sqlcmd = new wcf_rest_sqlcommand();
            sqlcmd.connect = connectString;
            sqlcmd.command = sQuery;
            sqlcmd.commandtype = "text";
        }
        private void InitCommand(string connectString, string spName, object[] parameterValues)
        {
            sqlcmd = new wcf_rest_sqlcommand();
            sqlcmd.connect = connectString;
            sqlcmd.command = spName;
            //sqlcmd.paramlist = new object[1][];

            //trong json phải chuyển kiểu DateTime sang giờ Utc => lấy giờ ngược lại khi Descr
            if (sqlcmd.jsonformat && parameterValues != null)
                for (int i = 0; i < parameterValues.Length; i++ )
                {
                    if (parameterValues[i] != null && parameterValues[i].GetType() == typeof(DateTime))
                    {
                        parameterValues[i] = DateTime.SpecifyKind((DateTime)parameterValues[i], DateTimeKind.Utc);
                        //if ((DateTime)parameterValues[i] <= DateTime.MinValue)
                        //    //parameterValues[i] = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
                        //    parameterValues[i] = (DateTime?)null;
                    }
                }

            //sqlcmd.paramlist = new object[1][1];
            sqlcmd.paramlist = parameterValues;
        }

        private void InitCommand(string connectString, string spName, object[][] parameterValues)
        {
            sqlcmd = new wcf_rest_sqlcommand();
            sqlcmd.connect = connectString;
            sqlcmd.command = spName;

            //trong json phải chuyển kiểu DateTime sang giờ Utc => lấy giờ ngược lại khi Descr
            if (sqlcmd.jsonformat && parameterValues != null)
                for (int i = 0; i < parameterValues.Length; i++)
                {
                    for (int j = 0; j < parameterValues[i].Length; j++)
                    {
                        if (parameterValues[i][j] != null && parameterValues[i][j].GetType() == typeof(DateTime))
                            parameterValues[i][j] = DateTime.SpecifyKind((DateTime)parameterValues[i][j], DateTimeKind.Utc);
                    }
                }

            //sqlcmd.paramlist = new object[1][1];
            sqlcmd.paramlist = parameterValues;
        }

        public DataSet ExecuteDataset(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public DataSet ExecuteDataset(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }


        //--Execute NoneQuery
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------

        public int ExecuteNonQuery(int iTransID, CommandType commandType, string commandText)
        {
            return 0;
        }

        public int ExecuteNonQuery(int iTransID, string spName, params object[] parameterValues)
        {
            return 0;
        }

        public int ExecuteNonQuery(int iTransID, string spName, object[][] parameterArray)
        {
            return 0;
        }

        public int ExecuteNonQuery(string connectString, CommandType commandType, string commandText)
        {
            return 0;
        }

        public int ExecuteNonQuery(string connectString, string spName, object[][] parameterArray)
        {
            return 0;
        }

        public int ExecuteNonQuery(string connectString, string spName, params object[] parameterValues)
        {
            InitCommand(connectString, spName, parameterValues);
            return Globals.Object_GetValue<int>(sqlcmd.ExecuteNonquery());
        }

        public int ExecuteNonQuery(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return 0;
        }

        public int ExecuteNonQuery(String connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return 0;
        }


        //--Execute DataRow
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------

        public DataRow ExecuteRow(int iTransID, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(int iTransID, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(int iTransID, string spName, params object[] parameterValues)
        {
            return null;
        }

        public DataRow ExecuteRow(int iTransID, string spName, params object[] parameterValues)
        {
            return null;
        }

        public DataRow ExecuteRow(string connectString, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(string connectString, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataRow ExecuteRow(string connectString, string spName, params object[] parameterValues)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(string connectString, string spName, params object[] parameterValues)
        {
            InitCommand(connectString, spName, parameterValues);
            DataTable dt = sqlcmd.ExecuteDataTable();
            if (dt != null && dt.Rows.Count > 0)
                return new DataRowEx(dt.Rows[0]);
            else
                return null;
        }

        public DataRow ExecuteRow(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public DataRow ExecuteRow(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public DataRowEx ExecuteRowEx(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }



        //--Execute Scalar
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        public object ExecuteScalar(int iTransID, CommandType commandType, string commandText)
        {
            return null;
        }

        public object ExecuteScalar(int iTransID, string spName, object[][] parameterArray)
        {
            return null;
        }

        public object ExecuteScalar(int iTransID, string spName, params object[] parameterValues)
        {
            return null;
        }

        public object ExecuteScalar(string connectString, CommandType commandType, string commandText)
        {
            if (commandType == CommandType.Text)
            {
                InitCommand(connectString, commandText);
                return sqlcmd.ExecuteScalar();
            }
            else
                return null;
        }

        public object ExecuteScalar(string connectString, string spName, object[][] parameterArray)
        {
            return null;
        }

        public object ExecuteScalar(string connectString, string spName, params object[] parameterValues)
        {
            InitCommand(connectString, spName, parameterValues);
            return sqlcmd.ExecuteScalar();
        }

        public object ExecuteScalar(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public object ExecuteScalar(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }


        //--Execute Datatable
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------
        public DataTable ExecuteTable(int iTransID, CommandType commandType, string commandText)
        {
            return null;
        }

        public DataTable ExecuteTable(int iTransID, string spName, params object[] parameterValues)
        {
            return null;
        }

        public DataTable ExecuteTable(string connectString, CommandType commandType, string commandText)
        {
            if (commandType == CommandType.Text)
            {
                InitCommand(connectString, commandText);
                return sqlcmd.ExecuteDataTable();
            }
            else
                return null;
        }

        public DataTable ExecuteTable(string connectString, string spName, params object[] parameterValues)
        {
            InitCommand(connectString, spName, parameterValues);
            return sqlcmd.ExecuteDataTable();
        }

        public DataTable ExecuteTable(int iTransID, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        public DataTable ExecuteTable(string connectString, CommandType commandType, string commandText, params object[] commandParameters)
        {
            return null;
        }

        //protected override void Finalize()
        //{
        //}

        public bool isServerUp(string endpoint)
        {
            return true;
        }

        public bool Rollback_Trans(int transID)
        {
            return true;
        }

        //private void М阄孩⬺얝ἶ✮ⳍ(IAsyncResult 쿪ቿ뇑ᬸ약粹ẍ坍)
        //{
        //}

        //private byte[] ꔲ鷊樂湭알᧒�(Dictionary<string, object> �鼐瑟䍵䓴슑低뮪)
        //{
        //}

        //private void ꔲ鷊樂湭알᧒�(params object[] 馺ʏ㚍憋躙⃫꾫)
        //{
        //}

        //private DataTable ⺦⠰�규쥤鹝ꭢ冡(DataRemoteReturn 㫷汆↏䜉慙ෙ멷꒖)
        //{
        //}

        //private object 꼅銬ᨉ맥峢癋ꎤ(DataRemoteReturn 㫷汆↏䜉慙ෙ멷꒖)
        //{
        //}

        //private void 奄잰혦럶萶矆䰔(string 뷲࣓䃮米♶솭㻈)
        //{
        //}

        //private object 釡בֿꮸ싖槨࿊䀏(int 枆꥞矣㮄鯻�ꮝ, string �ᯖ䇩⚮東౱釱, CommandType 䃎缹佸�䢐們亇ꩰ, string 겯膁ꕝ赸篘쇋砊瓼)
        //{
        //}

        //private object 釡בֿꮸ싖槨࿊䀏(int 枆꥞矣㮄鯻�ꮝ, string �ᯖ䇩⚮東౱釱, string 겆ద熿沟䛚귃盒, object[][] ꩀ홎䃒�괰Ἧ湻)
        //{
        //}

        //private object 釡בֿꮸ싖槨࿊䀏(int 枆꥞矣㮄鯻�ꮝ, string �ᯖ䇩⚮東౱釱, string 겆ద熿沟䛚귃盒, params object[] 餯쥂ᅀ乧籌賽炶멲)
        //{
        //}

        //private object 釡בֿꮸ싖槨࿊䀏(int 枆꥞矣㮄鯻�ꮝ, string �ᯖ䇩⚮東౱釱, CommandType 䃎缹佸�䢐們亇ꩰ, string 겯膁ꕝ赸篘쇋砊瓼, params object[] 馺ʏ㚍憋躙⃫꾫)
        //{
        //}

        //private void 㽚䌘嚢៏吁뽙겔()
        //{
        //}

        // Properties
        public string ConnectString
        {
            get
            {
                return _connectionString;
            }
            set
            {
                _connectionString = value;
            }
        }
    }
